*     *****************************************************************    *;
*     *****************************************************************    *;
*   Date:        June 2021   		                                       *;
*   Authors:     Dellmuth, Scholte, Tallberg, and Verhaegen                *;
*   Purpose:     Make dataset including all elites, citizens, and dyads    *;
*     *****************************************************************    *;
*     *****************************************************************    *;

set more off     
vers 16.1


***********************
* create elite datasets
***********************

use leggovelitedatacompleteAPSR.dta, clear

tab country
label define countries 1 "Global" 2 "Brazil" 3 "Philippines" 4 "Germany" 5 "South Africa" 6 "USA" 7 "Russia", modify
label values country countries
recode country 2=76 3=608 4=276 6=840 7=643
drop if country==1|country==5
label define label_country 76 "Brazil" 276 "Germany" 608 "Philippines" 643 "Russia" 840 "USA"
label values country label_country
tab country

gen respondent = _n
summarize respondent
gen resptype = 2


* create composite variables

sum confUN-confWTO
recode confUN 1=0 2=1 3=2 4=3
recode confIMF 1=0 2=1 3=2 4=3
recode confICC 1=0 2=1 3=2 4=3
recode confWB 1=0 2=1 3=2 4=3
recode confWHO 1=0 2=1 3=2 4=3
recode confWTO 1=0 2=1 3=2 4=3


replace age = surveyyear-yearbirth
tab age

tab polknow

gen ethliberal = (homo+divorce+sexmarriage+abort)/4

gen ordernation1 = postmodfirst
recode ordernation1 99=.a 2=0 3=0 4=0
tab ordernation1, missing

gen ordernation2 = postmodsecond
recode ordernation2 99=. 2=0 3=0 4=0
tab ordernation2, missing

gen comb_ordernation = ordernation1+ordernation2
tab comb_ordernation
* let range from TAN to GAL
recode comb_ordernation 2=0 1=0 0=1

gen TAN_GAL = ((ethliberal/9)+(immigrants/4)+(comb_ordernation/2))
mean TAN_GAL
* mean =2.079639
gen gal=1 if TAN_GAL>2.079639
replace gal=0 if TAN_GAL<2.079639
replace gal=. if TAN_GAL==.
tab gal

tab leftright
gen left=1 if leftright<=5.5
replace left=0 if leftright>=6
replace left=. if leftright==.
tab left

*drop left_gal left_tan right_gal right_tan center TAN_GAL_natel gal

gen left_gal=1 if (left==1 & gal==1)
gen left_tan=1 if (left==1 & gal==0)
gen right_gal=1 if (left==0 & gal==1)
gen right_tan=1 if (left==0 & gal==0)

replace left_gal=0 if (left_tan==1 |right_gal==1|right_tan==1)
replace left_tan=0 if (left_gal==1 |right_gal==1|right_tan==1)
replace right_gal=0 if (left_tan==1 |left_gal==1|right_tan==1)
replace right_tan=0 if (left_tan==1 |left_gal==1|right_gal==1)

sum left_gal left_tan right_gal right_tan

tab sex
tab gentrust
rename gentrust trust 


recode feelworld 4=0 3=1 1=3
recode feelcountry 4=0 3=1 1=3


gen confios= (confUN +confIMF +confICC +confWB +confWHO +confWTO)/6

compress

save "elites_BR DE RU US PHI - variables dropped for dyads recoded.dta", replace



***********************
* create WVS dataset
***********************

use WVS_Cross-national_Wave_7_Stata_v20200307APSR.dta, clear

tab B_COUNTRY, nol
rename B_COUNTRY country
keep if country==76|country==276|country==643|country==840|country==608
tab country
*label define label_country 76 "Brazil" 276 "Germany" 608 "Philippines" 643 "Russia" 840 "USA"
*label values country label_country
*tab country

gen respondent = (_n+1000)
summarize respondent
gen resptype = 1

rename Q275 education
rename Q50 finsathousehold
rename Q252 satis
tab edu, nol
tab finsathousehold, nol


* create composite variables

gen UNSCcorrect = Q91
recode UNSCcorrect 1 2 -1=0 3=1 -2=.
gen IMFcorrect =Q92
recode IMFcorrect 1=1 2 3 -1=0  -2=.
gen Amnestycorrect=Q93
recode Amnestycorrect 1 3 -1=0 2=1 -2=.

gen polknow = UNSCcorrect+IMFcorrect+Amnestycorrect
tab polknow
drop Q91 Q92 Q93 UNSCcorrect IMFcorrect Amnestycorrect

rename Q262 age
sum age 

tab Q57, nol
rename Q57 trust 
recode trust 2=0

rename Q257 feelcountry
tab feelcountry, nol
recode feelcountry 1=3 4=0 3=1

rename Q259 feelworld
tab feelworld, nol
recode feelworld 1=3 4=0 3=1

tab feelcountry
tab feelworld

gen ordernation1 = Q154
recode ordernation1 -3 -2 -1=. 2=0 3=0 4=0
tab ordernation1, missing

gen ordernation2 = Q155
recode ordernation2 -3 -2 -1=. 2=0 3=0 4=0
tab ordernation2, missing

gen comb_ordernation = ordernation1+ordernation2
tab comb_ordernation
recode comb_ordernation 2=0 1=0 0=1
tab comb_ordernation

rename Q182 homo
recode homo 1=0 2=1 3=2 4=3 5=4 6=5 7=6 8=7 9=8 10=9
tab homo
rename Q185 divorce
recode divorce 1=0 2=1 3=2 4=3 5=4 6=5 7=6 8=7 9=8 10=9
rename Q186 sexmarriage
recode sexmarriage 1=0 2=1 3=2 4=3 5=4 6=5 7=6 8=7 9=8 10=9
rename Q184 abort
recode abort 1=0 2=1 3=2 4=3 5=4 6=5 7=6 8=7 9=8 10=9

gen ethliberal = (homo+divorce+sexmarriage+abort)/4
tab Q121
rename Q121 immigrants
recode immigrants 1=0 2=1 3=2 4=3 5=4

gen TAN_GAL = ((ethliberal/9)+(immigrants/4)+(comb_ordernation/2))
mean TAN_GAL
* mean =1.256538
gen gal=1 if TAN_GAL>1.256538
replace gal=0 if TAN_GAL<1.256538
replace gal=. if TAN_GAL==.
tab gal

rename Q240 leftright
tab leftright
gen left=1 if leftright<=5.5
replace left=0 if leftright>=6
replace left=. if leftright==.
tab left

rename W_WEIGHT weight

rename Q260 sex
tab sex, nol
tab Q200, nol
tab Q71
tab Q71, nol
rename Q71 confgov
recode confgov 4=0 3=1 1=3
tab Q88
tab Q88, nol
rename Q88 confWHO
rename Q89 confWTO
rename Q87 confWB
rename Q83 confUN
rename Q84 confIMF
rename Q85 confICC
recode confWTO  -4/-1=. 4=0 3=1 1=3
recode confWHO  -4/-1=. 4=0 3=1 1=3
recode confIMF -4/-1=. 4=0 3=1 1=3
recode confUN -4/-1=. 4=0 3=1 1=3
recode confWB -4/-1=. 4=0 3=1 1=3
recode confICC -4/-1=. 4=0 3=1 1=3
tab confWHO
tab confIMF
tab confWTO
tab confWB
tab confUN
tab confICC, nol


gen confios= (confUN +confIMF +confICC +confWB +confWHO +confWTO)/6

label drop _all



compress

save "citizens_BR DE RU US PHI - variables dropped for dyads recoded.dta", replace



***********
* by country: merge elite with citizen dataset and make dyads; then drop the elite-based dyads
***********

use "citizens_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear 
append using "elites_BR DE RU US PHI - variables dropped for dyads recoded.dta", gen(datasource)

ed respondent resptype trust satis edu confios
label define resptype 1 "Citizen" 2 "Elite", modify
label values resptype resptype

save "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", replace



**********************************************
* split the elite and WVS samples by country, making dyads 
**********************************************

findit dyads // install the package to make dyads, note that the dyad command can take time

* create dyads for different countries, citizen data

use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
tab country, nol
keep if country==76  
order country-right_tan  respondent resptype
sort resptype respondent 
dyads respondent, dy(weight-confios)
sort respondent
ed respondent respondent_d
drop in 1/123
drop in 225992/1777432
sort respondent_d
drop in 1/7503 
compress
save BRdyads.dta, replace

use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
tab country, nol
keep if country==276 
order country-right_tan   resptype respondent
sort resptype respondent 
order country-right_tan   respondent resptype
dyads respondent, dy(weight-confios)
sort respondent
ed respondent respondent_d
drop in 1/122
drop in 195326/1361953
sort respondent_d
drop in 1/7381
compress
save DEdyads.dta, replace

cd "/Users/mariadellmuth/Desktop/comparison/makedyads/"
use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
*drop _est_wto _est_who _est_wb _est_us _est_un _est_rus _est_imf _est_icc _est_hs _est_ger _est_econ _est_bra _est_all
tab country, nol
keep if country==643
order country-right_tan   resptype respondent
order country-right_tan   respondent resptype
sort resptype respondent 
dyads respondent, dy(weight-confios)
sort respondent
ed respondent respondent_d
drop in 1/107
drop in 201152/1838296
sort respondent_d
drop in 1/5671 
compress
save RUdyads.dta, replace

cd "/Users/mariadellmuth/Desktop/comparison/makedyads/"
use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
*drop _est_wto _est_who _est_wb _est_us _est_un _est_rus _est_imf _est_icc _est_hs _est_ger _est_econ _est_bra _est_all
tab country, nol
keep if country==840 
sort resptype respondent
order country-right_tan   respondent resptype
dyads respondent, dy(weight-TAN_GAL)
sort respondent
ed respondent respondent_d
drop in 1/121
drop in 323973/3692282
sort respondent_d
drop in 1/7260
compress
save USdyads.dta, replace


cd "/Users/mariadellmuth/Desktop/comparison/makedyads/"
use "all_BR DE RU US PHI - variables dropped for dyads recoded.dta", clear
tab country, nol
keep if country==608 
sort resptype respondent
order country-right_tan   respondent resptype
dyads respondent, dy(weight-TAN_GAL)
sort respondent
ed respondent respondent_d
drop in 1/121
drop in 153661/873060
sort respondent_d
drop in 1/7260
compress
save PHIdyads.dta, replace



use USdyads.dta, clear
append using RUdyads.dta
append using DEdyads.dta
append using BRdyads.dta
append using PHIdyads.dta
save dyads.dta, replace

******************************
* You can now start analysing - see do file no 2
******************************



